<?php

namespace App\Http\Controllers\Home;

use App\Models\auth;
use App\Models\Fans_brand;
use App\Models\Member;
use App\Models\Room_admin;
use App\Models\Roomcategroy;
use App\Models\Roomgag;
use App\Models\Roomnum;
use function Sodium\add;

class ProfileController extends BaseController
{

    public function __construct(Member $member, auth $auth , Roomcategroy $roomcategroy , Roomnum $roomnum , Roomgag $roomgag , Room_admin $room_admin , Fans_brand $fans_brand)
    {
        parent::__construct();
        $this->member = $member;
        $this->auth = $auth;
        $this->roomcate = $roomcategroy;
        $this->room = $roomnum;
        $this->roomgag = $roomgag;
        $this->roomadmin = $room_admin;
        $this->fansbrand = $fans_brand;
    }

    /**
     * 检查是否实名认证
     */
    public function isTruenameAuth(){
         $loginId = session('loginId');
         $user = $this->member->memberInfo('isaudit',['id'=>$loginId]);
         if ($user['isaudit'] == 'y'){
              return 1;
         }else{
              return 0;
         }
    }

    /**
     * 个人中心首页
     */
    public function getIndex(){
        if (!session('loginId')){
            return redirect('index');
        }
        $user = $this->member->memberInfo('*',['id'=>session('loginId')]);
        $user['islogin'] = 1;
        $level =$this->member->getLevelInfo($user);
        if ($user['zhubo_flag']) {
            $zhuboinfo = $this->member->getZhuboInfo($user);
        }else{
            $zhuboinfo = [];
        }
        return view('Home.home.profile',compact('user','level','zhuboinfo'));
    }

    /**
     * 申请主播认证
     */
    public function getApplyforzhubo(){
         $user = $this->member->memberInfo('*',['id'=>session('loginId')]);
         $user['islogin'] = 1;
         // 检查是否实名认证
         $res = $this->isTruenameAuth();
         if($res) {
             // 判断是否申请了主播
             $room = $this->room->getOne(['uid'=>$user['id']]);
             if ($room){
                  $room = $room->toArray();
                  $data['isApply'] = $room;
             }
             $data['cate'] = $this->roomcate->getList();
         }
         $data['isauth'] = $res;
         return view('Home.home.room_apply',compact('user','data'));
    }

    /**
     * 实名认证
     */
    public function getAuthtruename(){
         $user = $this->member->memberInfo('*',['id'=>session('loginId')]);
         $user['islogin'] = 1;
         return view('Home.home.security_authfortruname',compact('user'));
    }

    /**
     * 验证身份证照片的有效性
     */
    public function postVerifyIdcardvalidity(){
         $base64 = $_POST['base64'];
         $dir = 'upload/temp/'.date('Ymd');
         $imagearr = [0=>$base64];
         $uplloadinfo = upImgs($imagearr,$dir);
         if ($uplloadinfo[0]) {
             // 进行图片有效性的验证
             $param['image_file'] ='@'.$uplloadinfo[0];
             $param['api_key'] = config('app.faceapp.app_key');
             $param['api_secret'] = config('app.faceapp.app_secret');
             $requestApi = 'https://api-cn.faceplusplus.com/cardpp/v1/ocridcard';
//             $res = $this->requestApi($requestApi,$param);
             $res['code'] = 200;
             $res['msg']  = '上传成功';
             $res['dest'] = $uplloadinfo[0];
             echo json_encode($res);
         }else{
             $res['code'] = 400;
             $res['msg']  = '图片上传错误';
             echo json_encode($res);
             die;
         }
    }

    public function requestApi($apiurl,$param){
        $curl = curl_init();
        $data = [
            'image_file' =>$param['image_file'],
            'api_key' => $param['api_key'],
            'api_secret' => $param['api_secret'],
        ];
        curl_setopt_array($curl, array(
            CURLOPT_URL => $apiurl,     //输入URL
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_SSL_VERIFYPEER=>'false',
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,   //输入api_key和api_secret
            CURLOPT_HTTPHEADER => array("cache-control: no-cache",),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    }

    /**
     * 保存认证信息
     */
    public function postSaveAuthInfo(){
           $res = $this->auth->addUserAuth();
           if ($res['code']) {
               return redirect('authresult');
           }else{
               return redirect('authresult?r=faill');
           }
    }

    /**
     * 查看认证结果
     */
    public function getAuthresult(){
        $user = $this->member->memberInfo('*',['id'=>session('loginId')]);
        $user['islogin'] = 1;
        if(isset($_GET['r']) && $_GET['r']=='faill') {
            $res['status'] = '3';
        }else{
            $res = $this->auth->getAuthInfo($user['id']);
        }
        return view('Home.home.auth_res',compact('user','res'));
    }

    /**
     * 申请直播间
     */
    public function postApplyfodo(){
         $res = $this->room->applyRoom(request());
         if ($res['code']==200) {
             return redirect('Applyfo_do');
         }
    }

    /**
     * 上传个人图片
     * 图片以每个用户的id文件夹存储
     */
    public function postUploadFace(){
        $res = $this->member->uploadface();
        echo json_encode($res);
    }

    /**
     * 修改基本信息
     * 图片以每个用户的id文件夹存储
     */
    public function postUploadMemberbaseInfo(){
        $res = $this->member->uploadmemberinfo(request());
        echo json_encode($res);
    }

    /**
     * 禁言列表
     */
    public function getGaglist(){
        $res = $this->roomgag->gaglist();
        echo json_encode($res);
    }

    /**
     * 取消禁言
     */
    public function postDelGag(){
         $res = $this->roomgag->delgag(request());
         echo json_encode($res);
    }

    /**
     * 房管列表
     */
    public function getRoomadminlist(){
         $res = $this->roomadmin->roomadminlist();
         echo json_encode($res);
    }

    /**
     *
     */
    public function getFanspai(){
         $res = $this->fansbrand->fanspailist();
         echo json_encode($res);
    }

    /**
     * 检查开通粉丝徽章权限
     */
    public function postCheckopenfans(){
         $res = $this->fansbrand->checkopenfan(request(['lev']));
         echo json_encode($res);
    }

    /**
     * 开通fans徽章
     */
    public function postAddfans(){
        $res = $this->fansbrand->addfansbrand(request());
        echo json_encode($res);
    }
}